#include "CharsetsDlg.h"

#include <QCompleter>
#include <QLineEdit>

#include <juffed/CharsetsSettings.h>
#include <juffed/JuffPlugin.h>
#include <juffed/PluginSettings.h>

CharsetsDlg::CharsetsDlg(JuffPlugin* plugin, QWidget* parent) 
: QDialog(parent)
, plugin_(plugin)
{
	ui.setupUi(this);
	
	ui.charsetCmb->setEditable(true);
	
	init();
	ui.filteredChk->setChecked(PluginSettings::getBoolValue(plugin_, "filtered"));
	
	QCompleter* completer = new QCompleter(ui.charsetCmb->model());
	completer->setCaseSensitivity(Qt::CaseInsensitive);
	ui.charsetCmb->setCompleter(completer);
	
	connect(ui.filteredChk, SIGNAL(toggled(bool)), SLOT(filterChecked(bool)));
}

void CharsetsDlg::init() {
	ui.charsetCmb->clear();
	
	QStringList list = CharsetsSettings::getCharsetsList();
	if ( PluginSettings::getBoolValue(plugin_, "filtered") ) {
		for ( int i = 0; i < list.count(); ++i ) {
			QString& charset = list[i];
			if ( !CharsetsSettings::charsetEnabled(charset) ) {
				list.removeAll(charset);
				--i;
			}
		}
	}
	ui.charsetCmb->addItems(list);
	ui.charsetCmb->lineEdit()->setText("");
}

QString CharsetsDlg::currentCharset() const {
	return ui.charsetCmb->currentText();
}

void CharsetsDlg::filterChecked(bool checked) {
	PluginSettings::setBoolValue(plugin_, "filtered", checked);
	init();
}
